package com.csht.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.csht.model.CshtTrading;
import com.csht.model.CshtTradingSuccess;
import com.csht.model.CshtUser;
import com.csht.page.PagerModel;
import com.csht.page.SystemContext;
import com.csht.service.CshtGoodsService;
import com.csht.service.CshtTradingService;
import com.csht.service.CshtTradingSuccessService;
import com.csht.vo.my.CshtTradingVo;

/**
 * 交易中的商品controller
 * @author chenhuiyan
 *
 */
@Controller
@RequestMapping(value = "/toMy")
public class TradingController {
	
	@Autowired
	private CshtTradingService cshtTradingService;
	
	@Autowired
	private CshtGoodsService cshtGoodsService;
	
	@Autowired
	private CshtTradingSuccessService cshtTradingSuccessService;

	/**
	 * 个人中心－交易中的商品页面
	 * @param model
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/trading")
	public String trading(ModelMap model, HttpServletRequest request) throws Exception{
		CshtUser cshtUser = (CshtUser) request.getSession().getAttribute("loginUser");
		Map<String, Integer> res = new HashMap<String,Integer>();
		res.put("userId", cshtUser.getUserId());
		res.put("messagePageSize", SystemContext.getMessagePageSize());
		res.put("offset", SystemContext.getOffset());
		PagerModel<CshtTradingVo> pagerModel = cshtTradingService.getTradingByUserId(res);
		model.addAttribute("pagerModel", pagerModel);
		return "/toMy/trading";
	}
	
	/**
	 * 交易完成
	 * @param tradingId
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/tradingGoodsToOk")
	public Map<String, Object> tradingGoodsToOk(@RequestParam(name = "tradingId") Integer tradingId) throws Exception{
		Map<String, Object> res = new HashMap<String, Object>();
		res.put("result", false);
		CshtTrading cshtTrading = cshtTradingService.selectTradingById(tradingId); //查找到交易中的商品
		cshtTradingService.deleteTradingById(tradingId); //删除商品
		//赋值到cshtTradingSuccess
		CshtTradingSuccess cshtTradingSuccess = new CshtTradingSuccess();
		cshtTradingSuccess.setBrandId(cshtTrading.getBrandId());
		cshtTradingSuccess.setGoodsId(cshtTrading.getGoodsId());
		cshtTradingSuccess.setOneMenuId(cshtTrading.getOneMenuId());
		cshtTradingSuccess.setThreeMenuId(cshtTrading.getThreeMenuId());
		cshtTradingSuccess.setTradingSuccessBrowseTime(cshtTrading.getTradingBrowseTime());
		cshtTradingSuccess.setTradingSuccessContent(cshtTrading.getTradingContent());
		cshtTradingSuccess.setTradingSuccessImgpath(cshtTrading.getTradingImgpath());
		cshtTradingSuccess.setTradingSuccessPayMoney(cshtTrading.getTradingPayMoney());
		cshtTradingSuccess.setTradingSuccessPrice(cshtTrading.getTradingPrice());
		cshtTradingSuccess.setTradingSuccessTitle(cshtTrading.getTradingTitle());
		cshtTradingSuccess.setTradingSuccessTradeSchool(cshtTrading.getTradingTradeSchool());
		cshtTradingSuccess.setTradingSuccessTradeWay(cshtTrading.getTradingTradeWay());
		cshtTradingSuccess.setTwoMenuId(cshtTrading.getTwoMenuId());
		cshtTradingSuccess.setUserId(cshtTrading.getUserId());
		cshtTradingSuccessService.insertTradingSuccessGoods(cshtTradingSuccess);
		cshtGoodsService.updateGoodsStateSuccessById(cshtTrading.getGoodsId());
		res.put("result", true);
		return res;
	}
	
	/**
	 * 重新上架
	 * @param tradingId
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/tradingGoodsToSell")
	public Map<String, Object> tradingGoodsToSell(@RequestParam(name = "tradingId") Integer tradingId) throws Exception{
		Map<String, Object> res = new HashMap<String, Object>();
		res.put("result", false);
		Integer goodsId = cshtTradingService.selectTradingGoodsIdById(tradingId); //查找原商品id
		cshtTradingService.deleteTradingById(tradingId); //删除记录
		cshtGoodsService.updateGoodsStateSellById(goodsId); //更新商品状态为"出售中"
		res.put("result", true);
		return res;
	}
}
